<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/mayioa/css/mayioa-ui.css}" rel="stylesheet"/>
<link th:href="@{/ajax/libs/datapicker/bootstrap-datetimepicker.min.css}" rel="stylesheet"/>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-plan-constraint-add">
			<!--<input name="planCfgDeptId"  type="hidden" id="treeId"/>-->

			<div class="form-group">
				<label class="col-sm-3 control-label">约束名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="planCstrName" id="planCstrName">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">约束对象：</label>
				<div class="col-sm-8">
					<select id="planCstrObj" class="form-control m-b" onchange="objectChange()" th:with="type=${@dict.getType('plan_constraint_obj')}">
                        <option value="">-&#45;&#45;请选择-&#45;&#45;</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">约束规则：</label>
				<div class="col-sm-8">
					<select id="planCstrRule" class="form-control m-b" th:with="type=${@dict.getType('plan_constraint_rule')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">约束键值：</label>
				<div class="col-sm-8">
					<select id="planCstrValue" name ="planCstrValue" class="form-control m-b"></select>
				</div>
			</div>

			<!--<div class="form-group">
				<label class="col-sm-3 control-label">是否参与系统审核：</label>
				<div class="col-sm-8">
					<div class="onoffswitch">
						<input type="checkbox" th:checked="true" class="onoffswitch-checkbox" id="planJoinSyscheck" name="planJoinSyscheck">
						<label class="onoffswitch-label" for="planJoinSyscheck">
							<span class="onoffswitch-inner"></span>
							<span class="onoffswitch-switch"></span>
						</label>
					</div>
				</div>
			</div>-->
		
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<textarea id="planCstrRemark" name="planCstrRemark" class="form-control"></textarea>
				</div>
			</div>
		</form>
	</div>
	
	<div th:include="include::footer"></div>
    <script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script th:src="@{/js/jquery-ui-1.10.4.min.js}"></script>
    <script th:src="@{/ajax/libs/iCheck/icheck.min.js}"></script>
    <script th:src="@{/ajax/libs//datapicker/bootstrap-datepicker.js}"></script>
    <script th:src="@{/ajax/libs/beautifyhtml/beautifyhtml.js}"></script>
    <script th:src="@{/js/vue.js}"></script>
	<script>
        var prefix = ctx + "plan/constraint";

        function objectChange(){
			window.constraintAdd.objectChange();
		}

		function submitHandler(){
	        if ($.validate.form()) {
	        	window.constraintAdd.add();
	        }
	    }

		window.constraintAdd=new Vue({
			el:"#form-plan-constraint-add",
			data:{
				itemOption:[]
			},
			created:function(){

			},
			mounted:function(){
				this.objectChange();
				this.formValidate();
			},
			methods: {
				formValidate: function(){
					var _this=this;
					$("#form-plan-constraint-add").validate({
                        rules:{
                            planCstrObj:{
                                required:true,
                                maxlength: 255,
                                remote: {
                                    url: prefix + "/checkPlanCfgNameUnique",
                                    type: "post",
                                    dataType: "json",
                                    data: {
                                        name : function() {
                                            return $.common.trim($("#planCstrObj").val());
                                        }
                                    },
                                    dataFilter: function(data, type) {
                                        return $.validate.unique(data);
                                    }
                                }
                            },
                            planCstrValue:{
                                required:true
                            },
                            messages: {
                                "planCstrObj": {
                                    remote: "约束对象已经存在"
                                }
                            }
                        }
                    });
				},
				objectChange: function(){
					var _this=this;
					var _urlTmp = "/selectCstrValuesByType";
					var _itemType = $("#planCstrObj option:selected").val();

					$.ajax({
		        		cache : true,
		        		type : "GET",
		        		url : prefix + _urlTmp,
		        		data : {
		        			"param": _itemType
		        		},
		        		async : false,
		        		error : function(request) {
		        			$.modal.alertError("系统错误");
		        		},
		        		success : function(data) {
		        			_this.itemOption=[];
		        			_this.itemOption=data;
		        			$("#planCstrValue").empty();
		        			var html_str="";
		        			for(var i=0;i<_this.itemOption.length;i++){
		        				html_str+='<option  value="'+_this.itemOption[i].itemId+'" >'+_this.itemOption[i].itemName+'</option>';
		        			}
		        			 $("#planCstrValue").append(html_str);
		        		}
		        	});
				},
				add:function() {
					var planCstrName = $("input[name='planCstrName']").val();
                    var planCstrObj = $("#planCstrObj option:selected").val();
                    var planCstrRule = $("#planCstrRule option:selected").val();
                    var planCstrValue = $("#planCstrValue option:selected").val();
                    <!--var planJoinSyscheck = $("input[name='planJoinSyscheck']").is(':checked') == true ? 'Y' : 'N';-->
                    var planCstrRemark = $("#planCstrRemark").val();
                    $.ajax({
                        cache : true,
                        type : "POST",
                        url : prefix + "/add",
                        data : {
                            "planCstrName": planCstrName,
                            "planCstrObj": planCstrObj,
                            "planCstrRule": planCstrRule,
                            "planCstrValue": planCstrValue,
                            <!--"planJoinSyscheck": planJoinSyscheck,-->
                            "planCstrRemark": planCstrRemark
                        },
                        async : false,
                        error : function(request) {
                            $.modal.alertError("系统错误");
                        },
                        success : function(data) {
                            $.operate.saveSuccess(data);
                        }
                    });
				}
			}
		})

    </script>
</body>
</html>
